package common

import (
	"ginProject/config/global"
	"ginProject/internal/model"
	"ginProject/internal/utils/aes"
	untils "ginProject/internal/utils/jwt"
	"github.com/dgrijalva/jwt-go"
	"go.uber.org/zap"
	"time"
)

func EncryptByAes(password string) string {
	aes := aes.NewAes()
	password, err := aes.EncryptByAes([]byte(password))
	if err != nil {
		zap.S().Error("AES Encrypt Failed:::", err)
	}
	return password
}

func DecryptByAes(password string, rePassword string) bool {
	aes := aes.NewAes()
	result, err := aes.DecryptByAes(password)
	if err != nil {
		zap.S().Error("Password Dencryption failed", err)
	}
	if string(result) != rePassword {
		return false
	}
	return true
}

func CreateToken(user model.User) (string, error) {
	//生成token
	jwtToken := untils.NewJWT(global.Config.Jwt.Secret)
	claims := untils.CustomClaims{
		ID:     user.ID,
		Mobile: user.Mobile,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + global.Config.Jwt.ExpireTime,
		},
	}
	token, err := jwtToken.CreateToken(claims)
	return token, err
}
